﻿Imports System.ComponentModel
Imports DevExpress.LookAndFeel
Imports System.Linq

Public Class fFacturasRecepcao
    Sub New()
        InitSkins()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub InitSkins()
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        UserLookAndFeel.Default.SetSkinStyle("Metropolis")
    End Sub
    Private Sub fFacturasEntrega_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DsTesourariaFornecedores1.FacturasRecepcao' table. You can move, or remove it, as needed.
        Me.FacturasRecepcaoTableAdapter.Fill(Me.DsTesourariaFornecedores1.FacturasRecepcao, GlobalVariables.Empresa)
        GridView1.BestFitColumns()
    End Sub
    Private Sub GridView1_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles GridView1.CellValueChanged
        If IsDBNull(GridView1.GetFocusedRowCellValue(colDataRecepcao)) = True Then
        Else
            Me.TblFacturasRecepcaoTableAdapter1.UpdateQuery(GridView1.GetFocusedRowCellValue(colDataRecepcao), GridView1.GetFocusedRowCellValue(colDocumentNo_), GlobalVariables.Empresa)
            EnvioEmail()
        End If
    End Sub


    Private Function EnvioEmail()

        Dim db As New LinqMainDataContext

        Dim EmailUser = (From c In db.tblUsers _
                        Where c.UserName = GridView1.GetFocusedRowCellValue(colPessoa).ToString _
                        Select c.Email).SingleOrDefault

        Dim Mensagem As New Net.Mail.MailMessage
        Dim Servidor As New Net.Mail.SmtpClient

        Mensagem.To.Add(EmailUser.ToString)


        Mensagem.From = New System.Net.Mail.MailAddress("appcontrol@sameca.pt", "appcontrol")
        Mensagem.Bcc.Add("appcontrol@sameca.pt")

        Mensagem.Subject = "Factura Recepcionada"
        Mensagem.IsBodyHtml = False
        Mensagem.Body = "Foi recepcionada a factura " & GridView1.GetFocusedRowCellValue(colExternalDocumentNo_) & " com a data " & GridView1.GetFocusedRowCellValue(colDocumentDate) & " do fornecedor " & GridView1.GetFocusedRowCellValue(colSearchName) & "."
        Servidor.Host = "srvex01"
        Servidor.Send(Mensagem)
    End Function
End Class